Skip to content

Method: bind(BindingGroup, Object, JTable)

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.thesefoolishthings.examples.dci.swing.swing;
28:
29: import javax.annotation.Nonnull;
30: import javax.swing.JList;
31: import javax.swing.JTable;
32: import org.jdesktop.beansbinding.BeanProperty;
33: import org.jdesktop.beansbinding.BindingGroup;
34: import org.jdesktop.observablecollections.ObservableList;
35: import org.jdesktop.swingbinding.SwingBindings;
36: import it.tidalwave.util.AsExtensions;
37: import it.tidalwave.thesefoolishthings.examples.dci.swing.role.ObservableListProvider;
38: import it.tidalwave.thesefoolishthings.examples.dci.swing.role.TableHeaderDescriptor;
39: import lombok.AccessLevel;
40: import lombok.NoArgsConstructor;
41: import lombok.experimental.ExtensionMethod;
42: import static it.tidalwave.thesefoolishthings.examples.dci.swing.role.ObservableListProvider._ObservableListProvider_;
43: import static it.tidalwave.thesefoolishthings.examples.dci.swing.role.TableHeaderDescriptor._TableHeaderDescriptor_;
44: import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.*;
45:
46: /***********************************************************************************************************************
47: *
48: * A facility to bind some Swing components to data.
49: *
50: * @author Fabrizio Giudici
51: *
52: **********************************************************************************************************************/
53: @ExtensionMethod(AsExtensions.class)
54: @NoArgsConstructor(access = AccessLevel.PRIVATE)
55: public final class Bindings
56: {
57: /*******************************************************************************************************************
58: *
59: * Binds a source to a {@link JList}. Two roles are used:
60: *
61: * <ol>
62: * <li>{@link ObservableListProvider} to retrieve the list model</li>
63: * <li>{@link HtmlRenderableListCellRenderer} to retrieve rendering for each item</li>
64: * </ol>
65: *
66: * @param bindings the {@link BindingGroup} to add the new binding to
67: * @param datum the datum
68: * @param jList the {@code JList}
69: *
70: ******************************************************************************************************************/
71: public static void bind (@Nonnull final BindingGroup bindings,
72: @Nonnull final Object datum,
73: @Nonnull final JList jList)
74: {
75: final ObservableList<?> ol = datum.as(_ObservableListProvider_).createObservableList();
76: bindings.addBinding(SwingBindings.createJListBinding(READ, ol, jList));
77: jList.setCellRenderer(new HtmlRenderableListCellRenderer());
78: }
79:
80: /*******************************************************************************************************************
81: *
82: * Binds a source to a {@link JTable}. Two roles are used:
83: *
84: * <ol>
85: * <li>{@link ObservableListProvider} to retrieve the list model</li>
86: * <li>{@link TableHeaderDescriptor} to retrieve the column definition</li>
87: * </ol>
88: *
89: * @param bindings the {@link BindingGroup} to add the new binding to
90: * @param datum the datum
91: * @param jTable the {@code JTable}
92: *
93: ******************************************************************************************************************/
94: public static <T> void bind (@Nonnull final BindingGroup bindings,
95: @Nonnull final Object datum,
96: @Nonnull final JTable jTable)
97: {
98: final ObservableList<T> ol = datum.as(_ObservableListProvider_).createObservableList();
99: final var tb = SwingBindings.createJTableBinding(READ_WRITE, ol, jTable);
100:
101: datum.as(_TableHeaderDescriptor_).getColumnDescriptors().forEach(cd ->
102: tb.addColumnBinding(BeanProperty.create(cd.getPropertyName())).setColumnName(cd.getHeaderName()));
103:
104: bindings.addBinding(tb);
105: }
106: }